package com.example.restful.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.restful.common.BaseService;
import com.example.restful.common.MaterialState;
import com.example.restful.domain.Borrow;
import com.example.restful.domain.Material;
import com.example.restful.domain.Personnel;
import com.example.restful.mapper.BorrowMapper;
import com.example.restful.mapper.MaterialMapper;
import com.example.restful.mapper.PersonnelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BorrowService implements BaseService<Borrow> {
    @Autowired
    BorrowMapper borrowMapper;
    @Autowired
    MaterialMapper materialMapper;
    @Autowired
    PersonnelMapper personnelMapper;
    @Autowired
    MaterialService materialService;

    @Override
    public List<Borrow> selectAll() {
        return borrowMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public Borrow selectById(int id) {
        return borrowMapper.selectById(id);
    }

    @Override
    public boolean addItem(Borrow item) {
        Personnel personnel = personnelMapper.selectById(item.getPersonnelId());
        Material material = materialMapper.selectById(item.getMaterialId());
        if(personnel == null || material == null || material.getState() != MaterialState.STORED){
            //不满足借出条件
            return false;
        }
        item.setEndDate(null);
        int count = borrowMapper.insert(item);
        if(count != 0){
            //添加借用记录成功， 修改material状态state
            material.setState(MaterialState.LOANED);
            materialService.updateById(item.getMaterialId(),material);
        }
        return borrowMapper.insert(item) != 0;
    }

    @Override
    public boolean deleteById(int id) {
        return borrowMapper.deleteById(id) != 0;
    }

    public boolean updateById(int id, Borrow borrow){
        borrow.setId(id);
        int count = 0;
        //修改后的借用记录， 如果存在endTime，表明物资已归还，需要修改material的state为0未借出
        Material material = materialMapper.selectById(borrow.getMaterialId());
        Personnel personnel = personnelMapper.selectById(borrow.getPersonnelId());
        if(material == null || personnel == null){
            return false;
        }
        material.setState(borrow.getEndDate() != null ? MaterialState.STORED : MaterialState.LOANED);
        if(materialMapper.updateById(material) != 0){
            count = borrowMapper.updateById(borrow);
        }
        return count != 0;
    }

    public List<Borrow> selectByMaterialId(int materialId) {
        QueryWrapper<Borrow> wrapper = new QueryWrapper<>();
        wrapper.eq("material_id",materialId);
        return borrowMapper.selectList(wrapper);
    }

    public List<Borrow> selectByPersonnelId(int personnelId) {
        QueryWrapper<Borrow> wrapper = new QueryWrapper<>();
        wrapper.eq("personnel_id",personnelId);
        return borrowMapper.selectList(wrapper);
    }

}
